<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mail; 
use DB;
use Crypt;

class Resetcontroller extends Controller
{
    //
    public function forget()
    {
    	return view('home.reset.email');
    }

    //send
    public function send(Request $request)
    {
    	//发送一封邮件
    	// Mail::raw('这里是邮件的内容', function($message){
    	// 	$message -> to('205001841@qq.com');
    	// 	$message -> subject('这是邮件的主题');
    	// });
    	// return $this -> text('email.home_forget');
    	
    	// 发送模版邮件
    	// Mail::send('email.forget',[],function($message){
    	// 	$message -> to('205001841@qq.com');
    	// 	$message -> subject('这是邮件的主题');
    	// });

    	$email = $request -> input('email');

        $arr = explode('@', $email);
        $tiaoEmail = 'http://mail.'.$arr[1];

     	$res = DB::table('users') -> where('email', $email) -> first();

    	if(!$res)
    	{
    		return back() -> with(['info' => '邮箱不存在']);
    	}
    	Mail::send('email.home_forget',['res' => $res],function($message) use ($email){
    		$message -> to($email);
    		$message -> subject('这是邮件的主题');
    	});

        return view('home.reset.tiao', ['tiaoEmail' => $tiaoEmail]);
    }

    public function find($token)
    {
    	// echo $token;
    	$res = DB::table('users') -> where("token", $token) -> first();
    	if(!$res)
    	{
    		return redirect('/home/reset/info') -> with(['info' => '这不是一个合法的邮件']);
    	}

    	return view('home.reset.newpass', ['res' => $res]);
    }

    public function info()
    {
    	return view('home.reset.info');
    }

    public function uppass(Request $request)
    {
    	$data = $request -> except('_token');
    	// dd($data);
    	$password = Crypt::encryptString($data['password']);

    	$res = DB::table('users') -> where('email', $data['email']) -> update(['password' => $password]);

    	if($res)
    	{
    		return redirect('/home/login') -> with(['info' => '修改成功']);
    	}else
    	{
    		return back() -> with(['info' => '修改失败']);
    	}

    }
}
